﻿using System;
using Microsoft.Xna.Framework;
using WindowsGame.Components;
using WindowsGame.Enums;

namespace WindowsGame.GameObjects
{
	public class Spaceship : GameObject
	{
		public void LoadContent(Vector4[] engineOffsets, Int32 preferredWindowWidth, Int32 preferredWindowHeight)
		{
			base.LoadContent();

			var thrustComponent = GetComponent<ThrustComponent>();
			thrustComponent.LoadContent(engineOffsets, preferredWindowWidth, preferredWindowHeight);
		}

		public void Update(TimeSpan elapsedTime, Direction direction, Boolean thrust, Boolean warp)
		{
			var physicsComponent = GetComponent<PhysicsComponent>();
			var thrustComponent = GetComponent<ThrustComponent>();

			// Rotate.
			physicsComponent.UpdateRotation(direction);

			// Thrust.
			Vector3 acceleration = thrustComponent.Update(elapsedTime, thrust, physicsComponent.Rotation, physicsComponent.Velocity);
			physicsComponent.UpdatePosition(elapsedTime, acceleration);

			// Warp.
			if (warp)
			{
				physicsComponent.UpdateVelocity(Vector3.Zero);
			}

			// Update.
			physicsComponent.Update();
		}

		public void Draw()
		{
			var modelComponent = GetComponent<ModelComponent>();
			var physicsComponent = GetComponent<PhysicsComponent>();
			var thrustComponent = GetComponent<ThrustComponent>();

			modelComponent.Draw(physicsComponent.WorldMatrix);
			thrustComponent.Draw(physicsComponent.WorldMatrix, physicsComponent.Rotation);
		}

		public ShipClass ShipClass
		{
			get
			{
				var modelComponent = GetComponent<ModelComponent>();
				return modelComponent.ShipClass;
			}
		}
	}
}